package com.xky.controller;

import com.xky.pojo.Result;
import com.xky.pojo.Student;
import com.xky.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class studentController {
    @Autowired
    private StudentService studentService;

    /**
     * 添加学生信息
     */
    @PostMapping("/student")
    public Result addStudent(@RequestBody Student student) {
        log.info("添加学生信息开始：{}", student);
        try {
            studentService.addStudent(student);
            log.info("添加学生信息成功：{}", student);
            return Result.success();
        } catch (Exception e) {
            log.error("添加学生信息失败：{}", e.getMessage(), e);
            return Result.error("添加学生失败：" + e.getMessage());
        }
    }

    /**
     * 删除学生信息
     */
    @DeleteMapping("/student/{ids}")
    public Result deleteStudent(@PathVariable Integer ids){
        log.info("删除学生信息：{}",ids);
        studentService.deleteStudent(ids);
        return Result.success();
    }

    /**
     * 修改学生信息
     */
    @PutMapping("/student")
    public Result updateStudent(@RequestBody Student student){
        log.info("修改学生信息：{}",student);
        studentService.updateStudent(student);
        return Result.success();
    }

    /**
     * 根据id查找学生信息
     */
    @GetMapping("/student/{id}")
    public Result listStudent(@PathVariable Integer id){
        log.info("根据id查找学生信息：{}",id);
      Student student = studentService.selectStudent(id);
        return Result.success(student);
    }

    /**
     * 查找所有学生信息
     */
    @GetMapping("/student")
    public Result selectAllStudent(){
        log.info("查找所有学生信息");
       List<Student> students = studentService.selectAllStudent();
       return Result.success(students);
    }

}

